package com.ljpz.sso.query.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljpz.common.service.RedisService;
import com.ljpz.sso.query.api.UserQueryService;
import com.ljpz.sso.query.bean.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by liujunchen on 2017/1/16.
 */
@Service
public class UserQueryServiceImpl  implements UserQueryService {
    @Autowired
    private RedisService redisService ;
    private  static  final ObjectMapper MAPPER = new ObjectMapper() ;
    private  static  final   Integer   REDIS_TIME=1800;


    @Override
    public User queryUserByToken(String token) {
        String key ="TOKEN_"+token  ;
        String jsonData = this.redisService.get(key)  ;
        if(StringUtils.isEmpty(jsonData)){
            //登录超时
            return  null ;
        }
        //重新设置redis中taken的生存时间
        this.redisService.expire(key,REDIS_TIME) ;
        try {
            return MAPPER.readValue(jsonData,User.class) ;
        }catch (Exception e ){
            e.printStackTrace();
        }
        return  null;
    }
}
